#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

struct Submission {
  int t, v;
};

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  for (cin >> T; T; T--) {
    int n, a, b, s;
    vector<vector<Submission>> submissions;

    cin >> n >> a >> b >> s;
    submissions.resize(n);
    for (int i = 0; i < s; i++) {
      int t, v_num;
      string p, v;

      cin >> t >> p >> v;
      if (v == "ac") {
        v_num = 0;
      } else if (v == "rj") {
        v_num = 1;
      } else {
        v_num = 2;
      }
      submissions[p[0] - 'A'].emplace_back(Submission{t, v_num});
    }

    int s_a = 0, s_b = 0;
    priority_queue<int> pq;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < submissions[i].size(); j++) {
        if (submissions[i][j].v == 0) {
          s_a++;
          s_b += submissions[i][j].t + j * 20;
          break;
        } else if (submissions[i][j].v == 2) {
          pq.emplace(-(submissions[i][j].t + j * 20));
          break;
        }
      }
    }

    int ans = 0;

    while (!pq.empty()) {
      if (s_a > a || s_a == a && s_b < b) {
        break;
      }
      ans++;
      s_a++;
      s_b -= pq.top();
      pq.pop();
    }
    if (s_a > a || s_a == a && s_b < b) {
      cout << ans << "\n";
    } else {
      cout << "-1\n";
    }
  }
  return 0;
}